/*
    仅用于秒针合作

    功能：通过秒针接口获取ip信息，ip信息提供给广告系统（目前播放器内广告会读取ip信息）
*/
window.F = window.F || {};
window.T = window.T || {};
window.T.trim = window.T.trim || function(source){
    var trimer = new RegExp("(^[\\s\\t\\xa0\\u3000]+)|([\\u3000\\xa0\\s\\t]+\x24)", "g");
    return String(source)
            .replace(trimer, "");
};
window.F.cookie = window.F.cookie || {
    get : function( name ) {
        // eg: "name1=value1; name2=value2; name3=value3; name4=value4"
        var c = document.cookie;
        if ( !c.length ) {
            return '';
        }
        var tp = c.split( '; ' );
        for ( var i = tp.length - 1; i >= 0; i-- ) {
            var tm = tp[ i ].split( '=' );
            if ( tm.length > 1 && tm[ 0 ] == name && tm[ 1 ] ) {
                return unescape( T.trim( tm[ 1 ] ) );
            }
        }
        return '';
    },
    set : function( name, value, day, domain, usehost ) {
        day = day || 365, domain = domain || '.fun.tv', usehost = usehost || 0;
        var expires = new Date();
        expires.setTime( (new Date()).getTime() + 3600 * 24 * 1000 * day );
        document.cookie = name + "=" + escape( value ) + "; path=/; " + (usehost ? 'host' : 'domain') + "=" + domain + (day == -1 ? '' : ";expires=" + expires.toGMTString());
    },
    del : function( name ) {
        this.set( name, '', -365, null, 0 );
        this.set( name, '', -365, document.location.host, 1 );
    }
};
window.F.loadJs = window.F.loadJs || function( url, fun, charset ) {
    charset = charset || 'utf-8', fun = fun || function() {};
    var sc = document.createElement("script");
    sc.setAttribute("charset",charset || "utf-8");
    sc.setAttribute("src", url);
    // 在 IE 下使用 onreadystatechange 事件监听加载完成, 其他浏览器使用 onload
    if ( !document.addEventListener ) {
        sc.onreadystatechange = function() {
            if ( sc.readyState == 'loaded' || sc.readyState == 'complete' ) {
                fun();
            }
        };
    } else {
        sc.onload = fun;
    }
    var insertScript = function(){
        var head = document.getElementsByTagName("head");
        if( head && head[0]){
            head[0].appendChild( sc );
        }
    };
    if( T.ready ){
        //support v8 framework
        T.ready( insertScript );
    }else{
        var i = 0;
        var intervalId = setInterval( function(){
            if( document.body ){
                insertScript();
                clearInterval( intervalId );
            }
            i += 1;
            if( i>30 ){
                //timeout 3s
                clearInterval( intervalId );
            }
        }, 100 )
    }
}
window.F.jsonp = window.F.jsonp || function( url, fun, callbackname ) {
    fun = fun || function() {};
    var key = "_R_"+ Math.floor(Math.random()*1000);
    // 当 script 加载完成后, 回调
    callbackname = callbackname || "callback";
    window[ key ] = function( s ) {
        // 执行回调
        fun( s );
        //注销回调函数，GC一下
        //window[key] = undefined;
        // 删除 jsonp 创建的 script 标签
        // 防止标签未执行完毕就被删除
        setTimeout( function() {
            var thescript = document.getElementsByTagName( "script" );
            for ( var i = thescript.length - 1, item; i >= 0; i-- ) {
                thescript[ i ] && (item = thescript[ i ]);
                if ( item && item.getAttribute( "src" ) && (item.getAttribute( "src" ).indexOf( key ) > -1) ) {
                    item.parentNode.removeChild( item );
                }
            }
        }, 100 );
    };
    // 100ms后 调用F.http.loadJs在页面创建一个script标签用于加载js信息
    setTimeout( function() {F.loadJs( url + (url.match( /\?/ ) ? '&' : '?') + callbackname + "=" + key );}, 100 );
};

var cookieName = "fck_mz_ip";
var ip = F.cookie.get( cookieName );
if( !ip ){
    F.jsonp("http://funshion.ipdx.cn.miaozhen.com/c.gif", function(data){
        data = data || {};
        ip = data.ip || "";
        ip = ip.replace(/[xX]/ig,".");
        //默认时间为2小时
        var second = parseInt(data.exptime+"") || 2*3600;
        var date = new Date(0);
        date.setSeconds( second );
        var expire = (date.valueOf() - new Date().valueOf())/(24*3600*1000);
        F.cookie.set(cookieName, ip, expire);
    });
}